package com.mo.springboot.reader;

import com.mo.springboot.entity.Teacher;
import com.mo.springboot.entity.enums.SexEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;


public class TeacherFieldSetMapper implements FieldSetMapper<Teacher> {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public Teacher mapFieldSet(FieldSet fieldSet) throws BindException {
        Teacher teacher = new Teacher();

        teacher.setId(fieldSet.readInt("id"));
        teacher.setName(fieldSet.readString("name"));
        teacher.setSex(SexEnum.getEnumFromString(fieldSet.readString("sex")));
        teacher.setBirthDate(fieldSet.readString("birthDate"));
        teacher.setHomeAddress(fieldSet.readString("homeAddress"));
        teacher.setSchool(fieldSet.readString("school"));
        teacher.setWorkingYears(fieldSet.readInt("workingYears"));

        teacher.setUpdateTime();
        logger.info("New teacher:" + teacher);
        return teacher;
    }
}
